package com.foreknow.elm.controller;

import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.BusinessService;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.BusinessServiceImpl;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {
    /**
     * 根据用户编号查询此用户所有购物车信息
     * @param request
     * @return
     * @throws Exception
     */
    public Object listCart(HttpServletRequest request) throws Exception{
        String userId = request.getParameter("userId");
        //调用service的方法
        CartService service = new CartServiceImpl();
        List<Cart> list = service.listCart(userId);
        return list;
    }

    /**
     * 向购物车表中添加一条记录
     * @param request
     * @return
     * @throws Exception
     */
    public Object saveCart(HttpServletRequest request) throws Exception{
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        CartService service = new CartServiceImpl();
        Integer integer = service.saveCart(userId,businessId,foodId);
        return integer;
    }

    /**
     * 根据用户编号、商家编号、食品编号更新数量
     * @param request
     * @return
     * @throws Exception
     */
    public Object updateCart(HttpServletRequest request) throws Exception{
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        Integer quantity = Integer.valueOf(request.getParameter("quantity"));
        CartService service = new CartServiceImpl();
        Integer integer = service.updateCart(userId,businessId,foodId,quantity);
        return integer;
    }

    /**
     * 根据用户编号、商家编号、食品编号删除购物车表中的一条食品记录
     * @param request
     * @return
     * @throws Exception
     */
    public Object  removeCart(HttpServletRequest request) throws Exception{
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        CartService service = new CartServiceImpl();
        Integer integer = service. removeCart(userId,businessId,foodId);
        return integer;
    }
}
